package com.dk.strategy;

import com.dk.config.CodePart;
import com.dk.mapper.AutoCodePartMapper;
import com.dk.model.AutoCodePart;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@CodePart(partType = "SERIALNO")
public class SeriainoStrategy implements CodeStrategyHandler {

    @Resource
    private AutoCodePartMapper autoCodePartMapper;

    @Override
    public StringBuffer code(AutoCodePart part) {
        StringBuffer buffer = new StringBuffer();
        //如果是流水号的话
        //获取流水号起始值
        Integer seriaStep = part.getSeriaStep();// 流水号步长
        Integer seriaStartNo = part.getSeriaStartNo();//流水号起始值
        Integer seriaNowNo = part.getSeriaNowNo();//流水号当前值
        Integer partLength = part.getPartLength();//获取流水号长度
        //是否开启循环
        if (StringUtils.equals(part.getCycleFlag(), "Y")) {
            //开启循环的话,获取循环方式,循环方式
            String cycleMethod = part.getCycleMethod();

            long l = System.currentTimeMillis();//当前时间
            Date updateTime = part.getUpdateTime();
            if (updateTime == null) {
                part.setUpdateTime(new Date());
            }
            long time = part.getUpdateTime().getTime();//当前修改时间
            //去判断时间是否满足条件去刷新当前值
            long cha = 0;
            if (StringUtils.equals(cycleMethod, "YEAR")) {
                //YEAR：按年
                cha = (l - time) / 1000 / 60 / 60 / 24 / 30 / 12;
            } else if (StringUtils.equals(cycleMethod, "MONTH")) {
                //MONTH：按月
                cha = (l - time) / 1000 / 60 / 60 / 24 / 30;
            } else if (StringUtils.equals(cycleMethod, "DAY")) {
                //DAY：按天
                cha = (l - time) / 1000 / 60 / 60 / 24;
            } else if (StringUtils.equals(cycleMethod, "HOUR")) {
                //HOUR：按小时
                cha = (l - time) / 1000 / 60 / 60;
            } else if (StringUtils.equals(cycleMethod, "MINITE")) {
                //MINITE：按分钟
                cha = (l - time) / 1000 / 60;
            } else if (StringUtils.equals(cycleMethod, "OTHER")) {
                //OTHER：按传入字符变
            }
            if (seriaNowNo == null) {
                part.setSeriaNowNo(seriaStartNo);//设置当前值
            }
            String s = part.getSeriaNowNo().toString();
            //判断当前流水号的长度与设置的长度是否符合
            int length = partLength - s.length();
            if(length < 0){
                return null;
            }
            for (int i = 0; i < length; i++) {
                buffer.append("0");
            }
            buffer.append(part.getSeriaNowNo());//拼接当前值
            if (cha >= 1) {
                //差大于1,刷新当前值并修改时间
                part.setSeriaNowNo(seriaStartNo);//再次修改当前值为初始化值
                part.setUpdateTime(new Date());
                //去修改数据库
                autoCodePartMapper.updateById(part);
            } else {
                //不足就不需要去修改时间
                part.setSeriaNowNo(part.getSeriaNowNo() + seriaStep);//再次修改当前值 + 步长
                //去修改数据库
                autoCodePartMapper.updateById(part);
            }
        } else {
            //未开启循环的话//判断当前值
            if (seriaNowNo == null) {
                part.setSeriaNowNo(seriaStartNo);//设置当前值
            }
            String s = part.getSeriaNowNo().toString();
            //判断当前流水号的长度与设置的长度是否符合
            int length = partLength - s.length();
            if(length < 0){
                return null;
            }
            for (int i = 0; i < length; i++) {
                buffer.append("0");
            }
            buffer.append(part.getSeriaNowNo());//拼接当前值
            part.setSeriaNowNo(part.getSeriaNowNo() + seriaStep);//再次修改当前值
            part.setUpdateTime(new Date());
            //去修改数据库
            autoCodePartMapper.updateById(part);
        }
        return buffer;
    }
}
